import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support.select import Select


class Base():
    def __init__(self, driver):
        self.driver = driver
        # self.driver = webdriver.Edge()

    def open_url(self, url):
        self.driver.get(url)

    def time(self, name):
        t = time.strftime('%Y%m%d%H%S%M', time.localtime())
        self.driver.save_screenshot(t+name)

    def find_element(self, loc):
        try:
            WebDriverWait(self.driver, 20, 0.5).until(lambda driver: driver.find_element(*loc).is_displayed())
            return self.driver.find_element(*loc)
        except Exception as e:
            print(f'元素定位不到，内部错误：{e}')
            # 错误截图：
            self.time('error.png')

    def text(self, loc):
        return self.find_element(loc).text

    def click(self, loc):
        self.find_element(loc).click()

    def send_keys(self, loc, info):
        self.find_element(loc).send_keys(info)

    # 浏览器事件
    # 返回
    def back(self):
        self.driver.back()
    # 前进
    def forward(self):
        self.driver.forward()
    # 刷新
    def refresh(self):
        self.driver.refresh()

    # 进入frame框架
    def frame(self, loc):
        self.driver.switch_to.frame(self.find_element(loc))
    # 退出frame框架
    def frame_tc(self):
        self.driver.switch_to.default_content()

    # 窗口切换
    def new_window(self):
        self.driver.switch_to.window(self.driver.window_handles[-1])

    # Select下拉框的封装（3种）
    def select_by_value(self, loc, value):
        select = Select(self.find_element(loc))
        select.select_by_value(value)
        # select.select_by_index(index)
        # select.select_by_visible_text(text)
    # 弹窗的封装

    # 接收
    def accept_alert(self):
        alert = self.driver.switch_to.alert
        alert.accept()

    # 取消
    def dismiss_alert(self):
        alert = self.driver.switch_to.alert
        alert.dismiss()

    # 获取弹窗文本信息
    def get_alert_text(self):
        alert = self.driver.switch_to.alert
        alert.accept()
        return alert.text

    # 弹窗输入文本并接受
    def send_text_to_alert(self, text):
        alert = self.driver.switch_to.alert
        alert.send_keys(text)
        alert.accept()

    # 滚动指定像素
    def scroll_to_px(self, px):
        self.driver.execute_script(f'window.scrollTo(0, {px});')

    # 滚动到页面顶部
    def scroll_to_top(self):
        self.driver.execute_script('windw.scrollTo(0,0)')

    # 滚动到页面底部
    def scroll_to_bottom(self):
        self.driver.execute_script('window.scrollTo(0, document.body.scrollHeight)')

    # 键盘事件的封装：
    # 回车事件
    def keys_enter(self, loc):
        self.find_element(loc).send_keys(Keys.ENTER)
    # 键盘全选事件

    # 复制、粘贴、删除、剪切、退格
    def keys_control_c(self, loc):
        self.find_element(loc).send_keys(Keys.CONTROL, 'c')
    def keys_control_v(self, loc):
        self.find_element(loc).send_keys(Keys.CONTROL, 'v')


    # 鼠标的悬停事件
    def move_to_element(self, loc):
        ActionChains(self.driver).move_to_element(self.find_element(loc)).perform()

    # 鼠标右击事件、双击事件、三击事件、长按事件、移动事件